﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySpaceID.SDK.Context;
using MySpaceID.SDK.Config;
using MySpaceID.SDK.OAuth.Common.Enums;
using System.IO;

namespace MySpaceID.SDK.Api
{
    public class RoaApi : BaseApi
    {
        /// <summary>
        /// <para>RoaApi is the class that encapsulates server-to-server calls to the MySpace ROA OpenSocial Version 0.9 Resources.</para>
        /// <para>Please refer to http://wiki.developer.myspace.com/index.php?title=Category:OpenSocial_Version_0.9 for full details on the MySpace SDK</para>
        /// </summary>
        public RoaApi(SecurityContext context) : base(context) 
        {
            this.Context.OAuthConsumer.ApiServerUri = Constants.MYSPACE_ROA_SERVER;
        }

        #region APIs

        /// <summary>
        /// <para>Adds a photo to an album.</para>
        /// <para>Resource: http://opensocial.myspace.com//roa/09/mediaitems/@me/@self</para>
        /// <para>See more details at http://wiki.developer.myspace.com/index.php?title=OpenSocial_0.9_MediaItems#Add_a_Photo_to_an_Album</para>
        /// </summary>
        /// <param name="userId">Id of the requestor</param>
        /// <param name="albumId">Id of the album that photo will go into.</param>
        /// <param name="caption">Caption for the photo.</param>
        /// <param name="photoData">A .jpg photo</param>
        public void AddPhoto(int userId, int albumId, string caption, byte[] photoData)
        {
            var requestBody = string.Empty;
            var albumVariable = string.Format("myspace.com.album.{0}", albumId);
            var queryParams = string.Format("xoauth_requestor_id={0}&type=IMAGE", userId);
            if (!string.IsNullOrEmpty(caption))
                queryParams += string.Format("&CAPTION={0}", caption);
            this.Context.MakeRequest(string.Format(RoaEndpoints.MEDIA_ITEMS, albumVariable, queryParams), ResponseFormatType.XML,
                HttpMethodType.POST, photoData, true);
        }

        /// <summary>
        /// <para>Adds a photo to an album.</para>
        /// <para>Resource: http://opensocial.myspace.com//roa/09/mediaitems/@me/@self</para>
        /// <para>See more details at http://wiki.developer.myspace.com/index.php?title=OpenSocial_0.9_MediaItems#Add_a_Photo_to_an_Album</para>
        /// </summary>
        /// <param name="userId">Id of the requestor</param>
        /// <param name="albumId">Id of the album that photo will go into.</param>
        /// <param name="caption">Caption for the photo.</param>
        /// <param name="filePath">File path to a .jpg photo. Please ensure your app has proper permission to reach this file.</param>
        public void AddPhoto(int userId, int albumId, string caption, string filePath)
        {
            try
            {
                using (FileStream fs = File.OpenRead(filePath))
                {
                    byte[] data = new byte[fs.Length];
                    fs.Read(data, 0, data.Length);
                    this.AddPhoto(userId, albumId, caption, data);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        #endregion
    }
}
